package cn.lili.modules.goods.serviceimpl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.lili.common.security.context.UserContext;
import cn.lili.modules.goods.entity.dos.Goods;
import cn.lili.modules.goods.entity.enums.GoodsAuthEnum;
import cn.lili.modules.goods.entity.enums.GoodsMarketEnum;
import cn.lili.modules.goods.mapper.GoodsStatisticsMapper;
import cn.lili.modules.goods.service.GoodsStatisticsService;
import cn.lili.modules.store.client.StoreClient;
import cn.lili.modules.store.entity.dos.Store;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品统计业务层实现
 *
 * @author Bulbasaur
 * @since 2020/12/9 11:30
 */
@Service
@RequiredArgsConstructor
public class GoodsStatisticsServiceImpl extends ServiceImpl<GoodsStatisticsMapper, Goods> implements GoodsStatisticsService {

    private final StoreClient storeClient;

    @Override
    public long goodsNum(GoodsMarketEnum goodsMarketEnum, GoodsAuthEnum goodsAuthEnum, String storeId) {
        LambdaQueryWrapper<Goods> queryWrapper = Wrappers.lambdaQuery();

        queryWrapper.eq(Goods::getDeleteFlag, false);

        if (goodsMarketEnum != null) {
            queryWrapper.eq(Goods::getMarketEnable, goodsMarketEnum.name());
        }
        if (goodsAuthEnum != null) {
            queryWrapper.eq(Goods::getAuthFlag, goodsAuthEnum.name());
        }
        queryWrapper.eq(CharSequenceUtil.isNotEmpty(storeId),
                Goods::getStoreId, storeId);

        return this.count(queryWrapper);
    }

    @Override
    public long todayUpperNum() {
        LambdaQueryWrapper<Goods> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Goods::getMarketEnable, GoodsMarketEnum.UPPER.name());
        queryWrapper.ge(Goods::getCreateTime, DateUtil.beginOfDay(new DateTime()));
        return this.count(queryWrapper);
    }


    @Override
    public long goodsNumByUrban(GoodsMarketEnum goodsMarketEnum, GoodsAuthEnum goodsAuthEnum, String storeId) {
        LambdaQueryWrapper<Goods> queryWrapper = Wrappers.lambdaQuery();

        queryWrapper.eq(Goods::getDeleteFlag, false);

        if (goodsMarketEnum != null) {
            queryWrapper.eq(Goods::getMarketEnable, goodsMarketEnum.name());
        }
        if (goodsAuthEnum != null) {
            queryWrapper.eq(Goods::getAuthFlag, goodsAuthEnum.name());
        }

        List<Store> storeList = storeClient.getSupplierListByUrbanId(UserContext.getCurrentUser().getId());
        if (!storeList.isEmpty()) {
            List<String> storeIds = storeList.stream().map(Store::getId).collect(Collectors.toList());
            queryWrapper.in(CollectionUtil.isNotEmpty(storeIds),
                    Goods::getStoreId, storeIds);
        }else {
            queryWrapper.eq(Goods::getStoreId, "");
        }


        return this.count(queryWrapper);
    }
}
